(function() {
	var $content = $('.listing .content');
	
	$("#dialog").dialog({
		autoOpen: false,
		modal: true,
		width: 1000
	});
	
	$("table.grid a").click(function() {
		var url = $(this).attr('href');
		window.loading.show($content);
		$.ajax({
			url:url,
			success: function(data)
			{
				$content.html(data);
			}
		});
		return false;
	});	
	
	/*
	$('td').hover(function(){
		$('.tools', this).show();
	}, function(){
		$('.tools', this).hide();
	});
	*/
	
	$('.distribute-tool').click(function(e){
		var $td = $(this).closest('td');
		var dayOfWeek = $td.attr('day-of-week');
		if (!dayOfWeek)
		{
			alert("没有在这个时间段上架或者计划上架的宝贝。");
			return false;
		}
		var hour = $td.attr('hour');
		var $table = $td.closest('table');
		var expected = $table.attr('expected');
		window.loading.show($content);
		$.ajax({
			url: 'well-distribute',
			data: {
				'listHour.dayOfWeek': dayOfWeek, 
				'listHour.hour': hour,
				expected: expected},
			type: 'POST',
			success: function(data){
				$content.html(data);
			}
		});
		return false;
	});
	
	$('.add-tool').click(function(e){
		var $td = $(this).closest('td');
		var time = $td.attr('time');
		var dayOfWeek = $td.attr('day-of-week');
		$.ajax({
			url: 'items-selector',
			type: 'POST',
			success: function(data) {
				var $dialog = $('#dialog');
				$dialog.html(data);
				$dialog.dialog('option', 'buttons', {
					确定 : function(){
						var selectedItems = '';
						var selectedRows = $('#selected-items tr', $dialog);
						selectedRows.each(function(){
							selectedItems = selectedItems + $(this).attr('num_iid') + ',';
						});
						$.ajax({
							url: 'schedule-listing',
							data: {numIids: selectedItems, dayOfWeek: dayOfWeek, time: time},
							type: 'POST',
							success: function(data, textStatus, jqXHR) {
								if (window.common.isOverflow(jqXHR))
								{
									return false;
								}
								$dialog.dialog('close');
							},
							error: function(jqXHR, textStatus, errorThrown) {
								alert(textStatus);
								var headers = jqXHR.getAllResponseHeaders();
								alert(headers);
								alert(errorThrown);
							}
						});
					},
					取消 : function(){
						$dialog.dialog('close');
					}
				});
				$dialog.dialog('open');;
			}
		});
		return false;
	});
	
	$('button.distribute-all').click(function(){
		var total = parseInt($('.period-view').attr('total'));
		if (total > 2000)
		{
			alert('对不起！您的宝贝数量太多，无法使用【一键均匀分布】。');
			return;
		}
		var b = confirm('【一键均匀分布】默认将宝贝分布于9:00 - 23:00之间。作为一种辅助手段，建议使用【一键均匀分布】进行粗略分布，然后再手动微调。如果您的宝贝已经分布得相对均匀，请直接手动微调，不建议使用【一键均匀分布】。确定要均匀分布所有出售中的宝贝吗？');
		if (b)
		{
			window.loading.show($content);
			$.ajax({
				url: 'well-distribute-all',
				success: function(data, textStatus, jqXHR)
				{
					if (window.common.isOverflow(jqXHR))
					{
						return false;
					}
					$('a#expected-view-tab').click();
				}
			});
			return false;
		}
	});
})();
